package com.briup.run.web.servlet;

import com.briup.run.bean.Memberinfo;
import com.briup.run.exception.CustomerException;
import com.briup.run.service.MemberInfoService;
import com.briup.run.service.impl.MemberInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @Auther: vanse(lc))
 * @Date: 2022/10/6-10-06-14:16
 * @Description：com.briup.run.web.servlet
 */
@WebServlet("/register")
@Slf4j   // 创建了Logger对象
public class MemberinfoRegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String url = "login.jsp";
        String error = "";

        // 接收的输入的验证码
        String authCodeFromWeb = req.getParameter("authCode");
        // 和后台的验证码 工具 session取出
        String authCodeFromServer = (String) session.getAttribute("authCode");

        // req.getParameterMap();
        // 1.接收数据
        String nickName = req.getParameter("nickName");
        String passwd = req.getParameter("passwd");
        String email = req.getParameter("email");
        String passwdQuestion = req.getParameter("passwdQuestion");
        String passwdAnswer = req.getParameter("passwdAnswer");
        String gender = req.getParameter("gender");
        String recommender = req.getParameter("recommender");
        String provinceCity = req.getParameter("provinceCity");
        // 数字格式化异常
        Long age = Long.parseLong(req.getParameter("age"));
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        Memberinfo memberInfo = Memberinfo.builder()
                .nickname(nickName)
                .password(passwd)
                .gender(gender)
                .age(age)
                .email(email)
                .provincecity(provinceCity)
                .address(address)
                .phone(phone)
                .passwordquestion(passwdQuestion)
                .passwordanswer(passwdAnswer)
                .recommender(recommender)
                .build();
        log.debug("memberInfo: {}", memberInfo);

        // 此依赖和lombok的set/get冲突
//        Map<String, String[]> map =
//                req.getParameterMap();
//        BeanUtils.populate(memberInfo,map);

        // 2.调用servcie 处理数据
        MemberInfoService memberInfoService = new MemberInfoServiceImpl();


        try {
            if(!authCodeFromServer.equals(authCodeFromWeb)){
                throw new CustomerException("验证码有误");
            }
            // 用完验证码 清除掉
            session.removeAttribute("authCode");
            // 验证码不通过 不注册
            memberInfoService.regsiter(memberInfo);
            // 3.根据service是否抛出异常 跳转页面
        } catch (Exception e) {
            // 此处未生效
            e.printStackTrace();
            url = "register.jsp";
            error = e.getMessage(); // 昵称已注册
            session.setAttribute("error",error);
        }
        // 请求转发 request error 错误提示
        // 重定向 session
                //  error 用完就移除
        resp.sendRedirect(url);
    }
}
